<template>
  <!-- 分页 -->
  <div id="Pagination">
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="pageSizesList"
      :background="background"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script lang="ts" setup name="Pagination">
// 标记--自动引入 import { computed } from 'vue'
// Pagination
const props = defineProps({
  total: {
    type: Number,
    required: true
  },
  page: {
    type: Number,
    default: 1, // 默认第一页
    required: true
  },
  pageSize: {
    type: Number,
    required: true,
    default: 10 // 默认每页10条
  },
  pageSizesList: {
    type: Array,
    default() {
      return [1, 2, 10, 20, 30, 50] // 默认显示可选的每页多少条数据
    }
  },
  layout: {
    type: String,
    default: '->,total, sizes, prev, pager, next, jumper'
  },
  background: {
    type: Boolean,
    default: true
  },
  autoScroll: {
    type: Boolean,
    default: true
  },
  hidden: {
    type: Boolean,
    default: false
  }
})

// 当前页
const currentPage: object = computed({
  get() {
    return props.page
  },
  set(val) {
    emit('page', val) // 改变的第几页的值赋值给父组件
  }
})
const pageSize: object = computed({
  get() {
    return props.pageSize
  },
  set(val) {
    emit('pageSize', val) // 改变的当前页几条数据的值赋值给父组件
  }
})

const emit = defineEmits(['pagination', 'page', 'pageSize'])

const handleSizeChange = (val: number) => {
  console.log(`val`, val)
  emit('pagination', { page: 1, pageSize: val })
}
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`)
  // props.pageSize
  emit('pagination', { page: val, pageSize: props.pageSize })
}
</script>

<style lang="scss" scoped>
#Pagination {
  .pagination-container {
    background: #fff;
    padding: 15px;
  }

  .pagination-container.hidden {
    display: none;
  }
}
</style>
